﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Threading;

namespace WcfSyncAsync.Service
{
    // To make attaching to the waiting client process easier/possible, we use Single/Single.
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    public class SimpleService : ISimpleService
    {
        public delegate string ProcessMessageDelegate(string request, int sleep);

        public string ProcessMessage(string request, int sleep)
        {
            Console.WriteLine("SimpleService.ProcessMessage - Going to sleep for " + sleep);
            Trace.WriteLine("SimpleService.ProcessMessage - Going to sleep for " + sleep);
            Thread.Sleep(sleep);
            return null;
        }
    }

    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract(Action = "ProcessMessage", AsyncPattern = false)]
        string ProcessMessage(string request, int sleep);
    }

    [ServiceContract]
    public interface ISimpleServiceAsync
    {
        [OperationContract(Action = "ProcessMessage", AsyncPattern = true)]
        IAsyncResult BeginProcessMessage(string request, int sleep, AsyncCallback callback, object state);

        string EndProcessMessage(IAsyncResult asyncResult);
    }
}